﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Data.Linq;
using CoolCode.Linq.Expressions;

namespace CoolCode.Data.Linq
{
    public static class TableExtensions
    {
        public static int Delete<TEntity>(this Table<TEntity> table, Expression<Func<TEntity, bool>> predicate) where TEntity : class
        {
            string tableName = table.Context.Mapping.GetTable(typeof(TEntity)).TableName;
            string command = String.Format("DELETE FROM {0}", tableName);

            ConditionBuilder conditionBuilder = new ConditionBuilder();
            conditionBuilder.Build(predicate.Body);

            if (!String.IsNullOrEmpty(conditionBuilder.Condition))
            {
                command += " WHERE " + conditionBuilder.Condition;
            }

            return table.Context.ExecuteCommand(command, conditionBuilder.Arguments);
        }

        //public static IEnumerable<TResult> Select<TSource, TResult>(this Table<TSource> table,  Expression<Func<TSource, TResult>> selector, bool withNoLock)
        //{
        //    var query = table.Select(selector) ;
        //    return  table.Context.ExecuteQuery<TResult>(query,withNoLock);
        //    //if (typeof(TSource) == typeof(TResult))
        //    //{
        //    //}
        //}
    }
}
